<#if notfound?? && notfound>
<div class="alert alert-danger" role="alert">
	<strong>错误！</strong> 无效的收款计划信息！
</div>
<#else>
<form class="contract-payin-form" action="${request.contextPath}/contract/payin-money-by-plan" method="post">
	<input type="hidden" name="planId" value="${data.id}" /> <input type="hidden" name="contract.id" value="${cid}" /> <input type="hidden" name="payOut" value="false" /> <input type="hidden" name="inAccount" value="true" />
	<div class="row">
		<div class="col-md-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">
							<span class="not-null-star">*</span> 资金来源
						</div>
					</div>
					<input type="text" class="form-control not-empty" name="source" value="${data.moneyFrom}" placeholder="最多50个汉字" readonly="readonly" />
				</div>
			</div>
		</div>
		<div class="col-md-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">
							<span class="not-null-star">*</span> 收款日期
						</div>
					</div>
					<input type="text" class="form-control not-empty datetimepicker" data-format="yyyy-MM-dd" data-foot="false" value="${(data.backTime?string('yyyy-MM-dd'))!''}" name="time" placeholder="请选择日期" readonly="readonly">
				</div>
			</div>
		</div>
		<div class="col-md-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">
							<span class="not-null-star">*</span> 实收金额
						</div>
					</div>
					<input type="text" class="form-control number not-empty" name="total" value="${data.total}" placeholder="只能是数字">
					<div class="input-group-addon">元</div>
				</div>
			</div>
		</div>
		<div class="col-md-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">发票</div>
					</div>
					<select class="form-control" name="invoice">
						<option value="" selected></option>
						<option value="true">已开</option>
						<option value="false">未开</option>
					</select>
				</div>
			</div>
		</div>
		<div class="col-md-12 for-invoice total hidden">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">
							<span class="not-null-star">*</span> 发票金额
						</div>
					</div>
					<input type="text" class="form-control number not-empty" name="invoiceTotal" value="${data.total}" placeholder="只能是数字">
					<div class="input-group-addon">元</div>
				</div>
			</div>
		</div>
		<div class="col-md-12 for-invoice desc hidden">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">发票说明</div>
					</div>
					<textarea rows="3" class="form-control" name="invoiceDes" placeholder="最多1000个中文"></textarea>
				</div>
			</div>
		</div>
		<div class="col-md-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">备注</div>
					</div>
					<textarea rows="5" class="form-control" name="remark" placeholder="最多1000个中文">${data.reason}</textarea>
				</div>
			</div>
		</div>
	</div>
	<hr>
	<div class="row">
		<div class="col-md-12 text-right">
			<div class="btn btn-primary btn-sm btn-submit">确定</div>
			&nbsp;
			<div class="btn btn-default btn-sm" onclick="Suredy.Modal.closeModal();">关闭</div>
		</div>
	</div>
</form>

<script type="text/javascript">
	require([ '${request.contextPath}/app/js/form-checker.js', 'suredyModal', 'jqueryForm', 'suredyDatetimepicker' ], function(formChecker, Modal) {
		{
			var $this = $('[name="invoice"]');
			var total = $('.for-invoice.total');
			var desc = $('.for-invoice.desc');

			if ($this.val() === 'true') {
				total.removeClass('hidden').find('[name]').prop({
					'disabled' : false
				});
				desc.removeClass('hidden').find('[name]').prop({
					'disabled' : false
				});
			} else if ($this.val() === 'false') {
				total.addClass('hidden').find('[name]').prop({
					'disabled' : true
				});
				desc.removeClass('hidden').find('[name]').prop({
					'disabled' : false
				});
			} else {
				total.addClass('hidden').find('[name]').prop({
					'disabled' : true
				});
				desc.addClass('hidden').find('[name]').prop({
					'disabled' : true
				});
			}
		}
		$('[name="invoice"]').on('change.invoice.status', function() {
			var $this = $(this);
			var total = $('.for-invoice.total');
			var desc = $('.for-invoice.desc');

			if ($this.val() === 'true') {
				total.removeClass('hidden').find('[name]').prop({
					'disabled' : false
				});
				desc.removeClass('hidden').find('[name]').prop({
					'disabled' : false
				});
			} else if ($this.val() === 'false') {
				total.addClass('hidden').find('[name]').prop({
					'disabled' : true
				});
				desc.removeClass('hidden').find('[name]').prop({
					'disabled' : false
				});
			} else {
				total.addClass('hidden').find('[name]').prop({
					'disabled' : true
				});
				desc.addClass('hidden').find('[name]').prop({
					'disabled' : true
				});
			}
		});

		$('form.contract-payin-form .btn-submit').on('click', function() {
			var form = $('form.contract-payin-form');

			// 校验表单
			if (!formChecker(form)) {
				return false;
			}

			var $this = $(this);
			var okBtn = $('form.contract-payin-form .btn-submit');

			if (okBtn.hasClass('disabled'))
				return false;

			okBtn.addClass('disabled');

			// 未开发票
			if ($('[name="invoice"]').val() === 'false')
				$('[name="invoiceTotal"]').val('');

			form.ajaxSubmit({
				dataType : "json",
				success : function(data, textStatus, jqXHR) {
					if (!data || !data.success) {
						var msg = (data && data.msg) || '收款失败!'
						alert(msg);
						okBtn.removeClass('disabled');
						return false;
					}

					refresh();

					Modal.closeModal();
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert('收款失败!\n' + errorThrown);
					okBtn.removeClass('disabled');
					return false;
				}
			});
		});
	});
</script>
</#if>
